package com.sy.service.permissions.impl;

import com.sy.bean.permissions.Menu;
import com.sy.bean.permissions.SaveMenu;
import com.sy.bean.permissions.ShowLinkMenu;
import com.sy.bean.permissions.StatusMenu;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.common.exception.BusinessException;
import com.sy.mapper.permissions.MenuMapper;
import com.sy.service.permissions.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu, MenuMapper> implements MenuService {
    @Autowired
    MenuMapper menuMapper;

    @Override
    public int save(SaveMenu entity) {
        return menuMapper.save(entity);
    }

    @Transactional
    @Override
    public int delete(int id) {
        int rs = menuMapper.queryCount(id);
        if (rs>0)
            throw new BusinessException("请先删除子菜单");
        rs = menuMapper.delete(id);
        if (rs<1)
            throw new BusinessException("删除菜单失败");
        return 1;
    }

    @Override
    public List<Map> tree() {
        return menuMapper.tree();
    }

    @Override
    public int editStatus(StatusMenu menu) {
        return menuMapper.editStatus(menu);
    }

    @Override
    public int editShowLink(ShowLinkMenu menu) {
        return menuMapper.editShowLink(menu);
    }


    @Transactional
    @Override
    public Map queryMenuByUserId(int id) {
        //一级二级菜单
        List list= menuMapper.queryMenuByUserId(id);
        //授权字符串
        String auth=menuMapper.queryAuths(id);

        Map map=new HashMap();
        map.put("menus",list);
        map.put("auths",auth);
        return map;

    }
}
